﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcGMaps.Models;

namespace MvcGMaps.DAL
{
    public class UnitOfWork : IDisposable
    {
        private MarkersDBEntities context;

        private MarkerRepository markerRepository;
        private GenericRepository<MarkerType> markerTypeRepository;
        private GenericRepository<Status> statusRepository;
        private UserRepository userRepository;

        public UnitOfWork()
        {
            context = new MarkersDBEntities();
        }

        public UnitOfWork(string nameOrConnectionString)
        {
            context = new MarkersDBEntities(nameOrConnectionString);
        }

        public MarkerRepository MarkerRepository
        {
            get
            {

                if (this.markerRepository == null)
                {
                    this.markerRepository = new MarkerRepository(context);
                }
                return markerRepository;
            }
        }

        public GenericRepository<MarkerType> MarkerTypeRepo
        {
            get
            {
                if (this.markerTypeRepository == null)
                {
                    this.markerTypeRepository = new GenericRepository<MarkerType>(context);
                }
                return markerTypeRepository;
            }
        }

        public GenericRepository<Status> StatusRepo
        {
            get
            {
                if (this.statusRepository == null)
                {
                    this.statusRepository = new GenericRepository<Status>(context);
                }
                return statusRepository;
            }
        }

        public UserRepository UserRepo
        {
            get
            {
                if (this.userRepository == null)
                {
                    this.userRepository = new UserRepository(context);
                }
                return userRepository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}